<?php

namespace App\Listeners;

use App\Events\SendMailEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailListener/* implements ShouldQueue*/
{
   // public $connection = 'redis';//任务应该发送到的队列的连接的名称
   // public $tries = 1;//任务最大尝试次数。
   // public $delay = 5;//延迟时间
   // public $queue = 'SendReminderEmail';//任务应该发送到的队列的名称

    /**
     * Create the event listener.
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     * @param  SendMailEvent  $event
     * @return void
     */
    public function handle(SendMailEvent $event)
    {
        $data = $event->data;
        $title = $data['title'];        //邮件标题
        $mailbox = $data['mailbox'];    //邮件地址
        $content = $data['content'];    //邮件内容

        if (empty($title) || empty($mailbox) || empty($content)) {
            return hello_error('参数错误！');
        } else {
            if (!checkdnsrr(array_pop(explode("@", $mailbox)), "MX")) {
                return hello_error('邮箱错误！');
            }
            \Illuminate\Support\Facades\Mail::raw($content, function ($message) use ($title, $mailbox) {
                $message->to($mailbox)->subject($title);
            });
            return hello_success('发送成功！');
        }
    }
}
